package com.emep.changzhi.analyse.service.basics;

import com.emep.changzhi.analyse.entity.basics.EPA;
import com.emep.changzhi.analyse.repository.basics.EPARepository;
import com.emep.changzhi.analyse.utils.Constant;
import org.bouncycastle.crypto.EphemeralKeyPair;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class EPAService {

    @Autowired
    EPARepository epaRepository;

    public Page<EPA> find(int pageIndex, int pageSize, Map<String, Object> params) {

        Page<EPA> epaPage = epaRepository.findAll(new EPASpec(params),
                PageRequest.of(pageIndex, pageSize, Sort.Direction.ASC, "epaId"));
        return epaPage;
    }

    public boolean add(EPA epa) {
        List<EPA> infos =epaRepository.findAllByNameAndIsDelete(epa.getName(), Constant.RESULT_DELETECODE_NO);
        if(infos.isEmpty()){
            epaRepository.save(epa);
            return true;
        }
        return false;
    }

    public EPA getEPA(long epaId){ return epaRepository.findAllByEpaId(epaId); }

    public boolean deleteEPA(EPA epa){
        EPA epa1 = epaRepository.save(epa);
        if(epa1 != null){
            return true;
        }else {
            return false;
        }
    }
    public boolean updateEPA(EPA epa){
        List<EPA> infos =epaRepository.findAllByNameAndEpaIdIsNotAndIsDelete(epa.getName(),epa.getEpaId(),Constant.RESULT_DELETECODE_NO);
        if(infos.isEmpty()){
            epaRepository.save(epa);
            return true;
        }
        return false;
    }

    private class EPASpec implements Specification<EPA> {
        private Map<String, Object> params = new HashMap<String, Object>();

        public EPASpec(Map<String, Object> params) {
            this.params = params;
        }

        @Override
        public Predicate toPredicate(Root<EPA> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
            List<Predicate> lstPredicates = new ArrayList<Predicate>();
            if (params.containsKey("isDelete")) {
                lstPredicates.add(cb.like(root.get("isDelete").as(String.class), params.get("isDelete").toString()));
            }
            if (params.containsKey("districtId")) {
                lstPredicates.add(cb.like(root.get("districtId").as(String.class), params.get("districtId").toString()));
            }
            if (params.containsKey("name")) {
                lstPredicates.add(cb.like(root.get("name").as(String.class), params.get("name").toString()));
            }
            Predicate[] arrayPredicates = new Predicate[lstPredicates.size()];
            return cb.and(lstPredicates.toArray(arrayPredicates));
        }
    }
}
